#include "SeqList.h"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->array = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->array == NULL)
	{
		printf("%s", strerror(errno));
		exit(-1);
	}
	else
	{
		ps->size = 0;
		ps->capicity = 4;
	}
}
void CheckCapicity(SeqList* ps)
{
	assert(ps);
	if ((ps->size) >= (ps->capicity))
	{
		ps->array = (SLDataType*)realloc(ps->array, sizeof(SLDataType)*(ps->capicity*2));
		if (ps->array == NULL)
		{
			printf("%s", strerror(errno));
			exit(-1);
		}
		else
		{
			ps->capicity = ps->capicity * 2;
		}
	}
}
void SeqListPushBack(SeqList* ps, SLDataType x)
{
	assert(ps);
	CheckCapicity(ps);
	ps->array[ps->size] = x;
	ps->size++;
	ps->capicity--;
}
void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < (ps->size); i++)
	{
		printf("%d ", (ps->array)[i]);
	}
	printf("\n");
}
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	ps->array[ps->size - 1] = 0;
	ps->size--;
	ps->capicity++;
}
void SeqListPushFront(SeqList* ps,SLDataType x)
{
	assert(ps);
	CheckCapicity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		(ps->array)[i] = (ps->array)[i - 1];
	}
	(ps->array)[0] = x;
	ps->size++;
	ps->capicity--;
}
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < ((ps->size) - 2); i++)
	{
		(ps->array)[i] = (ps->array)[i + 1];
	}
	ps->size--;
	ps->capicity++;
}
void SeqListFind(SeqList* ps,SLDataType x)
{
	assert(ps);
	for (int i = 0; i < (ps->size); i++)
	{
		if ((ps->array)[i] == x)
		{
			printf("The sign is %d ", i+1);
			return;
		}
	}
	printf("NOT FIND\n");
	return;
}
void SeqListInsert(SeqList* ps, SLDataType x,int pos)
{
	assert(ps);
	CheckCapicity(ps);
	for (int i = ps->size; i > pos-1; i--)
	{
		(ps->array)[i] = (ps->array)[i - 1];
	}
	(ps->array)[pos-1] = x;
	ps->size++;
	ps->capicity--;
}
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	for (int i = pos - 1; i < ((ps->size) - 1); i++)
	{
		(ps->array)[i] = (ps->array)[i + 1];
	}
	ps->size--;
	ps->capicity++;
}
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->array);
	ps->array = NULL;
	ps->size = 0;
	ps->capicity = 0;
}